package com.mashiro.system.service.impl;

import com.mashiro.common.utils.DateUtils;
import com.mashiro.system.domain.MoeMessage;
import com.mashiro.system.domain.status.MoeMessageStatus;
import com.mashiro.system.mapper.MoeMessageMapper;
import com.mashiro.system.service.IMoeMessageService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 消息Service业务层处理
 *
 * @author mashiro
 * @date 2023-04-14
 */
@Service
public class MoeMessageServiceImpl implements IMoeMessageService {
    @Resource
    private MoeMessageMapper moeMessageMapper;

    /**
     * 查询消息
     *
     * @param id 消息主键
     * @return 消息
     */
    @Override
    public MoeMessage selectMoeMessageById(Long id) {
        return moeMessageMapper.selectMoeMessageById(id);
    }

    /**
     * 查询消息列表
     *
     * @param moeMessage 消息
     * @return 消息
     */
    @Override
    public List<MoeMessage> selectMoeMessageList(MoeMessage moeMessage) {
        return moeMessageMapper.selectMoeMessageList(moeMessage);
    }

    /**
     * 新增消息
     *
     * @param moeMessage 消息
     * @return 结果
     */
    @Override
    public int insertMoeMessage(MoeMessage moeMessage) {
        moeMessage.setCreateTime(DateUtils.getNowDate());
        return moeMessageMapper.insertMoeMessage(moeMessage);
    }

    /**
     * 修改消息
     *
     * @param moeMessage 消息
     * @return 结果
     */
    @Override
    public int updateMoeMessage(MoeMessage moeMessage) {
        moeMessage.setUpdateTime(DateUtils.getNowDate());
        return moeMessageMapper.updateMoeMessage(moeMessage);
    }

    /**
     * 批量删除消息
     *
     * @param ids 需要删除的消息主键
     * @return 结果
     */
    @Override
    public int deleteMoeMessageByIds(Long[] ids) {
        return moeMessageMapper.deleteMoeMessageByIds(ids);
    }

    /**
     * 删除消息信息
     *
     * @param id 消息主键
     * @return 结果
     */
    @Override
    public int deleteMoeMessageById(Long id) {
        return moeMessageMapper.deleteMoeMessageById(id);
    }

    /**
     * 根据用户获取未读消息数量
     *
     * @param userId 用户
     * @return 结果
     */
    @Override
    public int countByUserId(Long userId) {
        return moeMessageMapper.countByReceiverId(userId);
    }

    /**
     * 批量修改消息状态
     *
     * @param userId     用户
     * @param messageIds 消息数组
     * @return 结果
     */
    @Override
    @Transactional
    public int read(Long userId, Long[] messageIds) {
        MoeMessage moeMessage = new MoeMessage();
        Date date = DateUtils.getNowDate();
        moeMessage.setReceiverId(userId);
        moeMessage.setUpdateTime(date);
        moeMessage.setReadStatus(MoeMessageStatus.READ.getValue());
        moeMessage.setReadTime(date);
        return moeMessageMapper.updateMoeMessageReadStatus(moeMessage, messageIds);
    }
}
